#include "TestScene.h"
#include "EffTestNode.h"
#include "USBTestNode.h"
#include "BilingOutTestNode.h"
#include "DialogTestNode.h"

/*********************| brief |*********************/

CCScene*TestScene::scene()
{
	CCScene* s = CCScene::create();
	TestScene* ts = TestScene::create();
	s->addChild(ts);
	return s;
}

TestScene::TestScene() : m_showNode(NULL)
{

}

TestScene::~TestScene()
{
}

bool TestScene::init()
{
	bool bRet = false;
	do {
		CC_BREAK_IF(!CCLayer::init());

		const CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		m_menu = CCMenu::create();
		m_menu->setPosition(CCPointZero);
		this->addChild(m_menu);
		addMenu("Effect(2个常用shader效果)");
		addMenu("USB(演示模板参见和混合模式)");
		addMenu("BillingBillingOut(例子和颜色采样)");
		addMenu("Dialog(几种常见的对话框)");

		CCMenuItemFont* backlbl = CCMenuItemFont::create("Back", this,  menu_selector(TestScene::backCB));
		backlbl->setAnchorPoint(ccp(1, 1));
		backlbl->setPosition(ccp(winSize.width, winSize.height));
		CCMenu* backMenu = CCMenu::create(backlbl, NULL);
		backMenu->setPosition(CCPointZero);
		this->addChild(backMenu, 1, 0);
		backMenu->setVisible(false);

		bRet = true;
	}while(0);
	return bRet;
}

void TestScene::showTestCB(CCObject* obj)
{
	CCNode* n = (CCNode*)obj;
	switch(n->getTag()) {
	case 0:
		m_showNode = EffTestNode::create();
		break;
	case 1:
		m_showNode = USBTestNode::create();
		break;
	case 2:
		m_showNode = BilingOutTestNode::create();
		break;
	case 3:
		m_showNode = DialogTestNode::create();
		break;
	default:
		n = NULL;
		break;
	}
	if (m_showNode) {
		CCMenu* backMenu = (CCMenu*)this->getChildByTag(0);
		backMenu->setVisible(true);
		m_menu->setVisible(false);
		this->addChild(m_showNode);
	}

}

void TestScene::backCB(CCObject* obj)
{
	this->removeChild(m_showNode);
	m_showNode = NULL;
	m_menu->setVisible(true);
	CCMenu* backMenu = (CCMenu*)this->getChildByTag(0);
	backMenu->setVisible(false);
}

void TestScene::addMenu(const char* name)
{
	if (name && name[0] != 0) {
		const CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		CCMenuItemFont* m = CCMenuItemFont::create(name, this, menu_selector(TestScene::showTestCB));
		m->setAnchorPoint(ccp(0.5, 1));
		unsigned int n = m_menu->getChildrenCount();
		m->setPosition(ccp(winSize.width / 2, winSize.height - 20 - (n *(m->fontSize() + 15))));
		m_menu->addChild(m, 0, n);
	}
}

